PluginProbe
Easy Hotel – Powerful Hotel Booking / trunk
Easy Hotel – Powerful Hotel Booking vtrunk
2.0.8 2.0.7 2.0.6 2.0.5 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.9.9 1.9.8 1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 All 110 releases
easy-hotel / admin / includes / classes / class.search copy.php

class.search copy.php in Easy Hotel – Powerful Hotel Booking trunk, at admin/includes/classes/class.search copy.php

484 lines 19.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3 class ESHB_Search {
4
5 private static $_instance = null;
6
7 public static function instance() {
8
9 if ( is_null( self::$_instance ) ) {
10 self::$_instance = new self();
11 }
12 return self::$_instance;
13
14 }
15
16 public function __construct() {
17
18 add_action( 'plugins_loaded', [ $this, 'init' ] );
19 add_action( "wp_ajax_nopriv_get_disabled_dates_by_accomodation_id", array ( $this, 'get_disabled_dates_by_accomodation_id' ) );
20 add_action( "wp_ajax_get_disabled_dates_by_accomodation_id", array ( $this, 'get_disabled_dates_by_accomodation_id' ) );
21
22 }
23
24 public function init() {
25
26 // Add Plugin actions
27 add_filter('theme_page_templates', [$this, 'register_eshb_page_template']);
28 //add_action('save_post', [$this, 'set_eshb_page_template']);
29 add_filter('template_include', [$this, 'load_eshb_template']);
30 //register_activation_hook(ESHB_PL_ROOT, [$this, 'create_eshb_page_on_activation']);
31
32 }
33
34 public function search_page_by_title( $page_title, $post_type = 'page' ) {
35 // Set up the arguments for WP_Query
36 $args = array(
37 'post_type' => $post_type,
38 'post_status' => 'publish', // Only fetch published pages
39 'title' => $page_title,
40 'posts_per_page' => 1, // Limit to one result
41 );
42
43 // Run the query
44 $query = new WP_Query( $args );
45
46 // Check if any post is found
47 if ( $query->have_posts() ) {
48 return $query->posts[0]; // Return the first found page
49 }
50
51 // Return false if no page is found
52 return false;
53 }
54
55 public function get_disabled_dates_by_accomodation_id($accomodation_id) {
56
57 // // Verify nonce for security
58 // if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'eshb_nonce')) {
59 // wp_send_json_error(['message' => 'Invalid nonce']);
60 // die();
61 // }
62
63 if (isset($_POST['accomodation_id'])) {
64 $accomodation_id = sanitize_text_field(wp_unslash($_POST['accomodation_id']));
65 }
66
67 $booked_dates = [];
68 $total_room_bookings_per_date = []; // Track room bookings per date
69 $eshb_settings = get_option('eshb_settings', []);
70
71 $holidays = isset($eshb_settings['holidays']) ? $eshb_settings['holidays'] : [];
72 $finally_holidays = [];
73
74 if(!empty($holidays)){
75 foreach ($holidays as $holiday) {
76 if (!isset($holiday['accomodation-ids']) || empty($holiday['accomodation-ids'])) {
77 // allow for all if not set accomodation ids
78 $finally_holidays[] = $holiday;
79 } elseif (is_array($holiday['accomodation-ids']) && in_array($accomodation_id, $holiday['accomodation-ids'])) {
80 // allow for selected accomodations
81 $finally_holidays[] = $holiday;
82 }
83 }
84 }
85
86
87
88 // Get total rooms allowed for this accommodation
89 $accomodation_metas = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true);
90 $allowed_total_rooms = isset($accomodation_metas['total_rooms']) ? intval($accomodation_metas['total_rooms']) : 0;
91 $allowed_check_in_day = isset($accomodation_metas['allowed_check_in_day']) ? $accomodation_metas['allowed_check_in_day'] : '';
92
93
94 $total_rooms = isset($accomodation_metas['total_rooms']) ? intval($accomodation_metas['total_rooms']) : 1;
95 $available_rooms = isset($accomodation_metas['available_rooms']) ? intval($accomodation_metas['available_rooms']) : $total_rooms;
96
97 $hotel_core = new ESHB_Core();
98 $required_min_nights = $hotel_core->get_eshb_required_max_min_nights($accomodation_id, 'min');
99 $required_max_nights = $hotel_core->get_eshb_required_max_min_nights($accomodation_id, 'max');
100
101
102 // Find all bookings for this accommodation
103 $bookings_args = [
104 'post_type' => 'eshb_booking',
105 'posts_per_page' => -1,
106 'meta_query' => [
107 'relation' => 'AND',
108 [
109 'key' => 'eshb_booking_metaboxes',
110 'compare' => 'EXISTS',
111 ],
112 ],
113 'fields' => 'ids',
114 ];
115
116 $bookings = new WP_Query($bookings_args);
117
118 if ($bookings->have_posts()) {
119 while ($bookings->have_posts()) {
120 $bookings->the_post();
121 $meta_values = get_post_meta(get_the_ID(), 'eshb_booking_metaboxes', true);
122 $booking_status = isset($meta_values['booking_status']) ? $meta_values['booking_status'] : '';
123
124 $completed_booking_status_maps = array(
125 'pending',
126 'processing',
127 'on-hold',
128 'completed'
129 );
130
131 // Ensure required keys exist
132 if (is_array($meta_values) && in_array($booking_status, $completed_booking_status_maps) && isset($meta_values['booking_accomodation_id'], $meta_values['booking_start_date'], $meta_values['booking_end_date'])) {
133
134 $booking_accomodation_id = intval($meta_values['booking_accomodation_id']);
135 $booking_start_date = $meta_values['booking_start_date'];
136 $booking_end_date = $meta_values['booking_end_date'];
137
138
139 $booked_accomodation_ids = [];
140
141 $translations = function_exists( 'pll_get_post_translations' ) ? pll_get_post_translations( $accomodation_id ) : [];
142
143 if ( ! empty( $translations ) ) {
144
145 $current_lang = pll_get_post_language( $booking_accomodation_id );
146
147 foreach ( $translations as $lang => $translated_post_id ) {
148
149 $booked_accomodation_ids[] = $translated_post_id;
150 }
151
152 // Example: fallback if no translations are available (after excluding current)
153 $booking_accomodation_id = $booked_accomodation_ids[0] ?? $booking_accomodation_id;
154
155
156 }
157
158
159 if ($accomodation_id == $booking_accomodation_id) {
160
161 // Convert dates to DateTime objects
162 $start_date = new DateTime($booking_start_date);
163 $end_date = new DateTime($booking_end_date);
164
165
166 $day_count = 0;
167 while ($start_date <= $end_date) {
168 $day_count++;
169
170 $formatted_date = $start_date->format('Y-m-d');
171
172 $booked_id = get_the_ID();
173 $booking_metas = get_post_meta($booked_id, 'eshb_booking_metaboxes', true);
174 $booked_room_quantity = isset($booking_metas['room_quantity']) ? intval($booking_metas['room_quantity']) : 0;
175
176
177 // Track total booked rooms per date
178 if (!isset($total_room_bookings_per_date[$formatted_date])) {
179 $total_room_bookings_per_date[$formatted_date] = 0;
180 }
181
182 $total_room_bookings_per_date[$formatted_date] += $booked_room_quantity;
183
184 $start_date->modify('+1 day');
185
186 }
187 // if(empty($allowed_check_in_day) || $allowed_check_in_day == 'all'){
188 // array_pop($total_room_bookings_per_date);
189 // }
190 }
191 }
192 }
193 }
194
195
196 $total_booked_rooms_in_date_ranges = 0;
197 $dates = [];
198
199 // Determine which dates should be disabled (fully booked)
200 foreach ($total_room_bookings_per_date as $date => $total_booked_rooms) {
201
202 //if ($allowed_total_rooms <= $total_booked_rooms) {
203 $date = new DateTime($date);
204 $booked_dates[] = $date->format('Y, m, d');
205
206 array_push($dates, array('dates' => $date, 'total_booked_rooms_in_date_ranges' => $total_booked_rooms));
207
208 // }
209
210 }
211
212
213 // Apply filter hook to allow external modifications
214 $booked_dates = apply_filters('eshb_modify_booked_dates', $booked_dates, $accomodation_id);
215
216
217 $is_week_plugin_actvated = get_option( 'eshb_week_activated' );
218
219 if(!$is_week_plugin_actvated){
220 $allowed_check_in_day = '';
221 }
222
223
224 $is_single_day_plugin_actvated = get_option( 'eshb_single_day_activated' );
225
226 $all_dates = [
227 'booked_dates' => $booked_dates,
228 'holiday_dates' => $finally_holidays,
229 'allowed_check_in_day' => $allowed_check_in_day,
230 'single_day' => false,
231 'min_nights' => $required_min_nights,
232 'max_nights' => $required_max_nights,
233 'total_room_bookings_per_date' => $total_room_bookings_per_date,
234 ];
235
236 if($is_single_day_plugin_actvated){
237 $all_dates['single_day'] = true;
238 }
239
240 if(isset($_POST['action']) && $_POST['action'] == 'get_disabled_dates_by_accomodation_id'){
241 wp_send_json_success($all_dates);
242 wp_die();
243 }else{
244 return $all_dates;
245 }
246
247 }
248
249 public function eshb_generate_dates_from_date_ranges($start_date, $end_date){
250 // $start_date = '2025, 05, 16'; // Format: YYYY, MM, DD
251 // $end_date = '2025, 05, 26';
252
253 $start = DateTime::createFromFormat('Y, m, d', $start_date);
254 $end = DateTime::createFromFormat('Y, m, d', $end_date);
255 $end->modify('+1 day'); // Include the end date in the range
256
257 $interval = new DateInterval('P1D'); // 1-day interval
258 $daterange = new DatePeriod($start, $interval, $end);
259
260 $dates_array = [];
261
262 foreach ($daterange as $date) {
263 $dates_array[] = $date->format('Y, m, d');
264 }
265 return $dates_array;
266 }
267
268 public function eshb_get_available_accomodation_ids($start_date, $end_date, $adult_quantity = 0, $children_quantity = 0, $rooms_quantity = 0) {
269 $available_accomodation_ids = [];
270
271 if ($start_date && $end_date) {
272
273 // Step 1: Get all accommodation IDs
274 $accomodations_args = array(
275 'post_type' => 'eshb_accomodation',
276 'posts_per_page' => -1,
277 'fields' => 'ids',
278 );
279
280 $accomodations = new WP_Query($accomodations_args);
281
282 if ($accomodations->have_posts()) {
283 $accomodation_ids = $accomodations->posts;
284
285 // Step 2: Get all bookings with overlapping dates
286 $bookings_args = array(
287 'post_type' => 'eshb_booking',
288 'posts_per_page' => -1,
289 'post_status' => 'publish',
290 'meta_query' => array(
291 array(
292 'key' => 'eshb_booking_metaboxes',
293 'compare' => 'EXISTS',
294 ),
295 ),
296 'fields' => 'ids',
297 );
298
299 $bookings = new WP_Query($bookings_args);
300 $accommodation_bookings = array(); // [accommodation_id => total_booked_rooms]
301 $booked_accomodation_ids = array();
302
303 if ($bookings->have_posts()) {
304 while ($bookings->have_posts()) {
305 $bookings->the_post();
306
307 $meta_values = get_post_meta(get_the_ID(), 'eshb_booking_metaboxes', true);
308 if (!is_array($meta_values)) continue;
309
310 $booking_accomodation_id = intval($meta_values['booking_accomodation_id']);
311 $booking_start_date = $meta_values['booking_start_date'];
312 $booking_end_date = $meta_values['booking_end_date'];
313 $booking_room_quantity = isset($meta_values['room_quantity']) ? intval($meta_values['room_quantity']) : 1;
314 $booking_status = isset($meta_values['booking_status']) ? $meta_values['booking_status'] : '';
315
316 $valid_statuses = array('pending', 'processing', 'on-hold', 'completed');
317
318 // Only count if booking status is valid and dates overlap
319 if (
320 in_array($booking_status, $valid_statuses) &&
321 $start_date <= $booking_end_date &&
322 $end_date >= $booking_start_date
323 ) {
324 if (!isset($accommodation_bookings[$booking_accomodation_id])) {
325 $accommodation_bookings[$booking_accomodation_id] = 0;
326 }
327
328 $accommodation_bookings[$booking_accomodation_id] += $booking_room_quantity;
329 }
330 }
331 wp_reset_postdata();
332
333 // Step 3: Mark accommodations as fully booked
334 foreach ($accommodation_bookings as $accommodation_id => $booked_rooms) {
335 $accomodation_metaboxes = get_post_meta($accommodation_id, 'eshb_accomodation_metaboxes', true);
336 $total_rooms = !empty($accomodation_metaboxes['total_rooms']) ? intval($accomodation_metaboxes['total_rooms']) : 1;
337
338 if ($booked_rooms >= $total_rooms) {
339 $booked_accomodation_ids[] = $accommodation_id;
340 }
341 }
342
343 // Step 4: Exclude fully booked accommodations
344 $available_accomodation_ids = array_diff($accomodation_ids, $booked_accomodation_ids);
345 } else {
346 // No bookings, all accommodations are available
347 $available_accomodation_ids = $accomodation_ids;
348 }
349
350 // Step 5: Filter by requested capacity
351 if (!empty($available_accomodation_ids)) {
352 foreach ($available_accomodation_ids as $key => $available_accomodation_id) {
353 $accomodation_metaboxes = get_post_meta($available_accomodation_id, 'eshb_accomodation_metaboxes', true);
354
355 $total_capacity = !empty($accomodation_metaboxes['total_capacity']) ? intval($accomodation_metaboxes['total_capacity']) : 1;
356 $adult_capacity = !empty($accomodation_metaboxes['adult_capacity']) ? intval($accomodation_metaboxes['adult_capacity']) : 1;
357 $children_capacity = !empty($accomodation_metaboxes['children_capacity']) ? intval($accomodation_metaboxes['children_capacity']) : 1;
358 $total_rooms = !empty($accomodation_metaboxes['total_rooms']) ? intval($accomodation_metaboxes['total_rooms']) : 1;
359
360 if (
361 $adult_quantity > $adult_capacity ||
362 $children_quantity > $children_capacity ||
363 $rooms_quantity > $total_rooms
364 ) {
365 unset($available_accomodation_ids[$key]);
366 }
367 }
368 }
369 }
370 }
371
372 return $available_accomodation_ids;
373 }
374
375 public function register_eshb_page_template($templates) {
376 $templates['easy-hotel-search.php'] = 'Easy Hotel Search';
377 $templates['easy-hotel-search-result.php'] = 'Easy Hotel Search Result';
378 return $templates;
379 }
380
381 public function set_eshb_page_template($page_id) {
382 if (get_post_type($page_id) == 'page') {
383 update_post_meta($page_id, '_wp_page_template', 'easy-hotel-search.php');
384 update_post_meta($page_id, '_wp_page_template', 'easy-hotel-search-result.php');
385 }
386 }
387
388 public function create_eshb_page_on_activation() {
389
390
391 // Create Search Page
392 // Define the page title and content
393 $search_page_title = 'Easy Hotel Search Result';
394 $search_page_content = '';
395
396 // Check if the page already exists
397 $page_check = $this->search_page_by_title($search_page_title);
398 if (!isset($page_check->ID)) {
399 // Create the page array
400 $new_page = array(
401 'post_title' => $search_page_title,
402 'post_content' => $search_page_content,
403 'post_status' => 'publish',
404 'post_type' => 'page',
405 'post_template' => 'easy-hotel-search.php'
406 );
407
408 // Insert the page into the database
409 $page_id = wp_insert_post($new_page);
410 }
411
412
413 // Define the page title and content
414 $search_result_page_title = 'Easy Hotel Search Result';
415 $search_result_page_content = '';
416
417 // Check if the page already exists
418 $page_check = $this->search_page_by_title($search_result_page_title);
419 if (!isset($page_check->ID)) {
420 // Create the page array
421 $new_page = array(
422 'post_title' => $search_result_page_title,
423 'post_content' => $search_result_page_content,
424 'post_status' => 'publish',
425 'post_type' => 'page',
426 'post_template' => 'easy-hotel-search-result.php'
427 );
428
429 // Insert the page into the database
430 $page_id = wp_insert_post($new_page);
431 }
432
433
434 }
435
436 public function load_eshb_template($template) {
437
438 global $post;
439
440 if (is_page() && $post->post_type == 'page') {
441 $template_meta = get_post_meta($post->ID, '_wp_page_template', true);
442
443
444 if ($template_meta == 'easy-hotel-search.php') {
445
446 $plugin_template = ESHB_PL_PATH . '/public/templates/easy-hotel-search.php';
447 $theme_template = get_stylesheet_directory() . '/easy-hotel-booking/easy-hotel-search.php';
448 $child_theme_template = get_template_directory() . '/easy-hotel-booking/easy-hotel-search.php';
449
450 if (file_exists($theme_template)) {
451 $template = $theme_template;
452 } elseif (file_exists($child_theme_template)) {
453 $template = $child_theme_template;
454 } else {
455 $template = $plugin_template;
456 }
457
458 }
459
460 if ($template_meta == 'easy-hotel-search-result.php') {
461
462 $plugin_template = ESHB_PL_PATH . '/public/templates/easy-hotel-search-result.php';
463 $theme_template = get_stylesheet_directory() . '/easy-hotel-booking/easy-hotel-search-result.php';
464 $child_theme_template = get_template_directory() . '/easy-hotel-booking/easy-hotel-search-result.php';
465
466 if (file_exists($theme_template)) {
467 $template = $theme_template;
468 } elseif (file_exists($child_theme_template)) {
469 $template = $child_theme_template;
470 } else {
471 $template = $plugin_template;
472 }
473
474 }
475
476
477 }
478 return $template;
479 }
480
481 }
482
483
484 ESHB_Search::instance();